import express from 'express';
import { v4 as uuid } from 'uuid';
import Streamer from './model/Streamer';
import { DYRecorder } from './DYRecorder';
import { book } from './DYRecorder';

const router = express.Router();
router.use(express.json());

// 创建新主播
router.post("/streamers", async (req, res) => {
  try {
    const { name, platform, roomId } = req.body;
    const newStreamer = await Streamer.create({
      id: uuid(),
      name,
      platform,
      roomId,
    });
    res.status(201).json(newStreamer);
  } catch (error) {
    res.status(400).json({ error });
  }
});

router.post("/stop/:id", async (req, res) => {
  const { id } = req.params;
  if (id && book[id] instanceof DYRecorder) {
    book[id].stop();
    delete book[id];
    res.sendStatus(200);
    return;
  }
  res.sendStatus(404);
});

// 获取所有主播
router.get("/streamers", async (req, res) => {
  try {
    const streamers = await Streamer.findAll();
    const ret = streamers.map(e => {
      let status = "offline";
      return {
        ...e.toJSON(),
        streaming: status,
        startTime: undefined
      };
    });
    res.json(ret);
  } catch (error) {
    res.status(400).json({ error });
  }
});

// 获取单个主播
router.get("/streamers/:id", async (req, res) => {
  try {
    const { id } = req.params;
    const streamer = await Streamer.findOne({
      where: { id }
    });
    
    if (!streamer) {
      return res.status(404).json({ error: "主播未找到" });
    }
    
    res.json(streamer);
  } catch (error) {
    res.status(400).json({ error });
  }
});

// 更新主播信息
router.put("/streamers/:id", async (req, res) => {
  try {
    const { id } = req.params;
    const { name, platform, roomId, status } = req.body;
    
    const [affectedCount] = await Streamer.update(
      {
        name,
        platform,
        roomId,
        status
      },
      { where: { id } }
    );
    
    if (affectedCount === 0) {
      return res.status(404).json({ error: "主播未找到" });
    }
    
    const updatedStreamer = await Streamer.findOne({ where: { id } });
    res.json(updatedStreamer);
  } catch (error) {
    res.status(400).json({ error });
  }
});

router.put("/stop/:id", async (req, res) => {
  const { id } = req.params;
  if (id && book[id] instanceof DYRecorder) {
    book[id].stop();
    delete book[id];
    res.sendStatus(200);
    return;
  }
  res.sendStatus(400);
});

// 删除主播
router.delete("/streamers/:id", async (req, res) => {
  try {
    const { id } = req.params;
    const deletedCount = await Streamer.destroy({
      where: { id }
    });
    
    if (deletedCount === 0) {
      return res.status(404).json({ error: "主播未找到" });
    }
    
    res.sendStatus(200);
  } catch (error) {
    res.status(400).json({ error });
  }
});

// 更新主播状态
router.put("/streamers/:id/status", async (req, res) => {
  try {
    const { id } = req.params;
    const { status } = req.body;
    
    const [affectedCount] = await Streamer.update(
      { status },
      { where: { id } }
    );
    
    if (affectedCount === 0) {
      return res.status(404).json({ error: "主播未找到" });
    }
    
    res.json({ id, status });
  } catch (error) {
    res.status(400).json({ error });
  }
});

export default router;